package com.frame.work.frame.deal.device0fbe;

import com.alibaba.fastjson.JSONObject;
import com.frame.work.common.constant.TypeEnum;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.config.SpringUtil;
import com.frame.work.entity.DeviceAssociatedInformation;
import com.frame.work.entity.TblSecurityLog;
import com.frame.work.frame.deal.base.BaseFrame;
import com.frame.work.mapper.SelectDeviceMapper;
import com.frame.work.service.TblSecurityLogService;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 智能门磁传感器0FBE8102
 */
@Data
public class ZhiNengMenCiChuanGanQi_0FBE8102 extends BaseFrame {

    private String switchState;

    public ZhiNengMenCiChuanGanQi_0FBE8102(String gatewayMac, String data) {

        //将基础数据进行处理
        super(gatewayMac, data);

        //处理此设备特有的数据

        String reportType = StringUtils.substring(data, 40, 42);
        String switchState = StringUtils.substring(data, data.length() - 8, data.length() - 6);
        String fangChaiState = StringUtils.substring(data, data.length() - 6, data.length() - 4);
        // String alarmState = StringUtils.substring(data,data.length() - 2, data.length());
        //String fangChaiState = StringUtils.substring(data,data.length() - 6, data.length() - 4);
        //数据库操作 begin
        String reportNum = "00";
        String type = TypeEnum.NORMAL.code();
        if (!reportNum.equals(reportType)) {
            if (1 == 1 || "01".equals(switchState) || "01".equals(fangChaiState)) {


                //获取mapper进行数据库操作
                SelectDeviceMapper testMapper = (SelectDeviceMapper) SpringUtil.getBean(SelectDeviceMapper.class);
                List<DeviceAssociatedInformation> list = testMapper.getDevice1(this.getDeviceMac());
                if (list.size() > 0) {
                    DeviceAssociatedInformation deviceRelation = list.get(0);
                    String homeName = "";
                    String ls_text = "";
                    if (deviceRelation.getHomeName() != null && deviceRelation.getHomeName() != "") {
                        homeName = deviceRelation.getHomeName();
                    }
                    String deviceName = deviceRelation.getDeviceName();
                    if (deviceName == "" || deviceName == null) {
                        deviceName = "智能门磁";
                    }


                    String content = deviceName + "打开";
                    type = TypeEnum.STATE.code();


                    //保存报警信息
                    TblSecurityLog tblSecurityLog = new TblSecurityLog();
                    tblSecurityLog.setGatewayMac(this.getGatewayMac());
                    tblSecurityLog.setDeviceMac(this.getDeviceMac());
                    tblSecurityLog.setContent(content);
                    tblSecurityLog.setType(type);
                    //获取userId
                    String userId = deviceRelation.getUserId();
                    tblSecurityLog.setUserId(userId);
                    TblSecurityLogService tblSecurityLogService = (TblSecurityLogService) SpringUtil.getBean(TblSecurityLogService.class);
                    tblSecurityLogService.insert(tblSecurityLog);
                }
            }
        }

        //数据库操作 end

        this.setSwitchState(switchState);

        //将actions信息放入json对象中
        Map<String, Object> actions = new HashMap();
        actions.put("reportType", reportType);
        actions.put("switchState", switchState);
        actions.put("fangChaiState", fangChaiState);
        this.setActions(actions);


        //处理好的json对象
        String json = JSONObject.toJSONString(this);
        //将数据发送到mqtt队列中
        JsonToMqtt.sendMQTT(gatewayMac, json);

    }

}
